<template>
    <div class="user-management-container">

        <!-- 右侧用户管理 -->
        <div class="right-panel">
            <el-form :inline="true" :model="dataps" class="search-form" v-if="isshows">
                <el-form-item label="组织编码">
                    <el-input v-model="dataps.OrcanCord" clearable />
                </el-form-item>
                <el-form-item label="组织架构">
                    <el-input v-model="dataps.OrcanName" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="GetRole">查询</el-button>
                </el-form-item>
            </el-form>

            <!-- 搜索表单 -->


            <!-- 操作按钮区域 -->
            <div style="margin-left: 1200px;">
                <el-button type="success" @click="CreateRole(1)">新增</el-button>
                <el-icon style="margin-left: 10px; cursor: pointer;">
                    <ZoomOut @click="show" />
                </el-icon>
                <el-icon style="margin-left: 10px; cursor: pointer;">
                    <Refresh @click="GetRole()" />
                </el-icon>
            </div>
            <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="oranId" border
                default-expand-all>
                <el-table-column prop="date" label="Date" sortable />
                <el-table-column prop="oranCord" label="组织编码" sortable />
                <el-table-column prop="oranCordName" label="组织框架" sortable />
                <el-table-column label="操作" sortable>
                    <template v-slot="scoped">
                        <el-button type="primary" @click="CreateRole(scoped.row)">编辑</el-button>
                        <el-button type=" success" @click="DelOrcan(scoped.row.oranId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination-container">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="page.pageIndex" :page-sizes="[2, 4, 6, 8]" :page-size="page.pageSize"
                    layout="total, sizes, prev, pager, next, jumper" :total="page.totalCount">
                </el-pagination>
            </div>
        </div>
    </div>

    <el-dialog v-model="dialogFormVisible" :title="models.title" width="500">
        <el-form :model="forms">
            <el-form-item label="组织编码">
                <el-input v-model="forms.oranCord" autocomplete="off" />
            </el-form-item>
            <el-form-item label="组织架构">
                <el-input v-model="forms.oranCordName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="上级部门">
                <el-cascader v-model="forms.organProId" :options="options" :props="cascaderProps"
                    :show-all-levels="false" clearable />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="CreateRoles(1)" v-if="models.isAdd">
                    新增
                </el-button>
                <el-button type="primary" @click="CreateRoles(2)" v-else>
                    更新
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue';
import { APIs } from '../../Http/http'
import { useCounterStore } from '../../stores/counter'
import { EditPen, Delete, ZoomOut, Refresh } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import moment, { max } from 'moment';
const store = useCounterStore();
const page = ref({
    pageIndex: 1,
    pageSize: 10,
    total: 0,
    totalCount: 0
})

const isshows = ref(false)
const show = () => {

    if (isshows.value == false) {
        isshows.value = true;

    }
    else {
        isshows.value = false;

    }
}
const dataps = ref({
    OrcanCord: "",
    OrcanName: ""
})

const tableData = ref<any>([

])

const handleChange = (value: any) => {

    console.log(value)
}

const GetRole = () => {
    APIs.get("/api/Read/OrcanQuery", {
        params: {
            pageIndex: page.value.pageIndex,
            pageSize: page.value.pageSize,
            total: page.value.total,
            totalCount: page.value.totalCount,
            OrcanCord: dataps.value.OrcanCord,
            OrcanName: dataps.value.OrcanName,
        }
    }).then(res => {
        tableData.value = res.data.data.data;
        page.value.total = res.data.data.total;
        page.value.totalCount = res.data.data.totalCount;
    })
}

const DelOrcan = (mid: number) => {
    ElMessageBox.confirm(
        '确定要删除吗?',
        '警告',
        {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            APIs.delete("/api/Write/OrcanDel", { data: { id: mid } }).then(res => {
                if (res.data.code == 100) {
                    ElMessage.success("删除成功");
                    GetRole();
                }
                else {
                    ElMessage.error("删除成功")
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Delete canceled',
            })
        })

}

const forms = ref({
    "createName": "string",
    "createTime": "2025-09-08T01:16:06.711Z",
    "updateName": "string",
    "updateTime": "2025-09-08T01:16:06.711Z",
    "deleteName": "string",
    "deleteTime": "2025-09-08T01:16:06.711Z",
    "isDel": true,
    "oranId": 0,
    "oranCord": "string",
    "oranCordName": "string",
    "deptId": 0,
    "organProId": ''
})

const dialogFormVisible = ref(false)
const models = ref({
    title: "",
    isAdd: false
})
const CreateRole = (account: any) => {
    if (account == 1) {
        dialogFormVisible.value = true
        forms.value.oranCord = "",
            forms.value.oranCordName = "",
            forms.value.deptId = 0,
            models.value.title = "新增组织"
        models.value.isAdd = true
    }
    else {
        dialogFormVisible.value = true
        forms.value.oranCordName = account.oranCordName
        forms.value.oranId = account.oranId
        forms.value.deptId = account.deptId
        forms.value.oranCord = account.oranCord
        forms.value.organProId = account.organProId
        forms.value.createTime = account.createTime
        forms.value.isDel = account.isDel
        models.value.title = "更新组织"
        models.value.isAdd = false
    }

}



const CreateRoles = (id: number) => {
    if (id == 1) {
        APIs.post("/api/Write/OrcanCreate", { data: forms.value }).then(res => {
            if (res.data.code == 100) {
                ElMessage.success("新增成功");
                GetRole();
                dialogFormVisible.value = false
            }
            else {
                ElMessage.error("新增失败");
                dialogFormVisible.value = false
            }
        })

    }
    else {
        APIs.put("/api/Write/OrcanUpdate", { data: forms.value }).then(res => {
            if (res.data.code == 100) {
                ElMessage.success("更新成功")
                GetRole();
                dialogFormVisible.value = false
            }
            else {
                ElMessage.error("更新失败")
                dialogFormVisible.value = false
            }
        })
    }

}

const options = ref<any>([])
const cascaderProps = ref({
    value: 'value',
    label: 'label',
    children: 'children',
    checkStrictly: false,
    emitPath: false
})

const GetDept = () => {
    APIs.get("/api/Read/DeptTree").then(res => {
        options.value = res.data.data;
    })
}


// 分页处理函数
const handleSizeChange = (val: number) => {
    page.value.pageSize = val;
    GetRole();
}

const handleCurrentChange = (val: number) => {
    page.value.pageIndex = val;

    GetRole();
}

onMounted(() => {
    GetRole();
    GetDept();
})

</script>